package com.zjsru.oneDay202205;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/6/12
 * 查找和替换模式
 * 输入：words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
 * 输出：["mee","aqq"]
 * 解释：
 * "mee" 与模式匹配，因为存在排列 {a -> m, b -> e, ...}。
 * "ccc" 与模式不匹配，因为 {a -> c, b -> c, ...} 不是排列。
 * 因为 a 和 b 映射到同一个字母。
 *
 */
public class findAndReplacePattern {
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> ans = new ArrayList<>();
        int[] map = new int[26], vis = new int[26];
        for (String s:words) {
            Arrays.fill(map,-1);
            Arrays.fill(vis,0);
            boolean ok = true;
            for (int i = 0; i < pattern.length(); i++) {
                int c1 = s.charAt(i) - 'a', c2 = pattern.charAt(i) - 'a';
                //判断是否一个字符串的每个值是否已存在，vis[c2] == 0为了说明是否与上一个值相同
                if(map[c1] == -1 && vis[c2] == 0){
                    map[c1] = c2;
                    vis[c2] = 1;
                }else if (map[c1] != c2){
                    ok = false;
                }
            }
            if (ok) ans.add(s);
        }
        return ans;
    }
  
    public static void main(String[] args) {
        findAndReplacePattern findAndReplacePattern = new findAndReplacePattern();
        String[] words = new String[]{"abc","deq","mee","aqq","dkd","ccc"};
        String pattern = "abb";
        System.out.println(findAndReplacePattern.findAndReplacePattern(words, pattern));
    }
}
